package com.allianz.webapps.secondController;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;


import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.druid.util.StringUtils;
import com.allianz.frame.utils.DateUtils;
import com.allianz.webapps.common.CustomerContextHolder;
import com.allianz.webapps.https.interfaces.type001.pojo.User;
import com.allianz.webapps.service.PolicyMaintenanceService;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sun.org.apache.bcel.internal.generic.NEW;

@Controller
@RequestMapping(value="/mainten")
public class MaintenanceController {

	@Autowired
	private PolicyMaintenanceService  maintenService;
	@Autowired
	private ObjectMapper obMapper;
	
	SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
	
	@RequestMapping(value="/getMaintenList")
	@ResponseBody
	public List<Map<String,Object>> getMainList(@RequestParam(value="jsonParam",required=false)String jsonParam,HttpSession session) throws JsonParseException, JsonMappingException, IOException, ParseException{
		User user = (User) session.getAttribute("username");
		String u = user.getAgentId();
		long uc = Long.parseLong(u);
		 String status=null;
		 String entryFrom=null;
		 String entryTo = null;
		 String policyRef = null;
		 String phName =null;
		 String polCode=null;
		if (!StringUtils.isEmpty(jsonParam)) {
			@SuppressWarnings("unchecked")
			Map<String,Object>param=obMapper.readValue(jsonParam, Map.class);
			status=(String) param.get("status");
			entryFrom=(String) param.get("entryFrom");
			entryTo = (String) param.get("entryTo");
			policyRef = (String) param.get("policyRef");
			phName = (String) param.get("phName");
			polCode=(String) param.get("polCode");
			/* if ("结案".equals(status)||"拒绝".equals(status)||"撤销".equals(status)) {
				entryFrom=sdf.format(DateUtils.getDate(new Date(), 5, -5));
				entryTo=sdf.format(new Date());
				CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_ALLIANZ);
				return maintenService.getPolicyMainten(uc, status, entryFrom, entryTo, policyRef, phName);
			}*/
			CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_ALLIANZ);
			return maintenService.getPolicyMainten(uc, status, entryFrom, entryTo, policyRef, phName,polCode);
			
		}
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_ALLIANZ);
		return maintenService.getPolicyMainten(uc, status, entryFrom, entryTo, policyRef, phName,polCode);
	}
	

	@RequestMapping(value="/getMaintenDetail")
	@ResponseBody
	public  List<Map<String,Object>> getDeteil(@RequestParam(value="jsonParam",required=false)String jsonParam,HttpSession session) throws Exception{
		if (!StringUtils.isEmpty(jsonParam)) {
		@SuppressWarnings("unchecked")
		Map<String,Object>param=obMapper.readValue(jsonParam, Map.class);
		String taskIdStr = (String) param.get("taskId");
		long taskId = Long.parseLong(taskIdStr);
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_ALLIANZ);
		return maintenService.getPolicyMaintenDetail(taskId);
		}
		throw new Exception("缺少必要参数!");
	}
	
	@RequestMapping(value="/getMaintenDetailByContractId")
	@ResponseBody
	public  List<Map<String,Object>> getDeteilByContractId(@RequestParam(value="jsonParam",required=false)String jsonParam,HttpSession session) throws Exception{
		if (!StringUtils.isEmpty(jsonParam)) {
			@SuppressWarnings("unchecked")
			Map<String,Object>param=obMapper.readValue(jsonParam, Map.class);
			String contract_Id = String.valueOf(param.get("contractId"));
			long contractId = Long.parseLong(contract_Id);
			CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_ALLIANZ);
			return maintenService.getPolicyMaintenDetailByContractId(contractId);
		}
		throw new Exception("缺少必要参数!");
	}
}
